#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "LPC23xx.H"
#include "bas_fun.h"
#include "lcd_fun.h"

#ifdef LCD_FUN                
#include "uart_fun.h"
#include "net_fun.h"
#include "aio_fun.h"
#include "rtu_file.h"

#ifdef PLC_FUN
#include "plc_fun.h"
#endif
#ifdef SM_RS
#include "sm_rs.h"
#endif
#ifdef SMB_RS
#include "smb_rs.h"
#endif

#ifdef SDNP3
#include "sdnp3\sd3_rs.h"
#endif

U8 lcd_rdy=0;
char tlcdmsg[32];
unsigned short LcdLightOffSec;

//////////////////////////////////////////////////////////////
#define KEY_ENTER 0x01
#define KEY_ESC   0x02
#define KEY_RIGHT 0x04
#define KEY_DOWN  0x08
				
#define max_key_buf  10
unsigned char key_buf[max_key_buf];
unsigned char key_h,key_t;
unsigned char key_cnt;
unsigned char key_rc;
void key_buf_write(char c)
{
    if (key_h>=max_key_buf) key_h=0;
    key_buf[key_h++]=c;
}

//unsigned char key_buf_rdy(void)
//{
//    if (key_h>=key_t) return(key_h-key_t);
//    else return((key_h+max_key_buf)-key_t);
//}

unsigned char get_key(void)
{
    if (key_h==key_t) return(0);
    if(key_t>=max_key_buf) key_t=0;
    return((key_rc=key_buf[key_t++]));
}

unsigned char key0;
void key_scan(void)
{
  	    unsigned char key;

		key_dly=0;
		key=lcd_key;  // ~LCD_KEY_IN();
		if (key==0) { 
		    if (++key0>5) { key0=5; key_cnt=0; return; }
	    }
		else {
		    key0=0;
		    if (key_cnt>=1) return;
			
		    //Dprintf("#KC=%02X\n",key);
			switch(key) {
                case KEY_ENTER: // 0x01
                case KEY_ESC  : // 0x02
                case KEY_RIGHT: // 0x04
                case KEY_DOWN : // 0x08
				      LcdLightOffSec=0;
		              break;
				default : 
				   return;
			}			
		    key_cnt++;
			if (lcd_rdy)  {
			    if (!lcd_light_flag) { LCD_LIGHT(1); return; }			
			}
            key_buf_write(key);
		}
}

///////////////////////////////////////////////////////////////////////////////
#define CURSOR_OFF    (LCD_comm(0x0C))
#define CURSOR_ON     (LCD_comm(0x0E))
#define CURSOR_BLINK  (LCD_comm(0x0F))

#define X_NUM 12
#define Y_NUM  2
S8  LCD_msg[Y_NUM][X_NUM+1];

U8  _x,_y,x_blink,y_blink;
U8  Y_C[Y_NUM]={0x80,0xC0};
U8  ymsg1[X_NUM+1];
U8  ymsg2[X_NUM+1];
void LCD_yx(char y,char x)
{
  if (x<1) x=1;
	if (x>X_NUM) x=X_NUM;
	if (y<1) y=1;
	if (y>Y_NUM) y=Y_NUM;
	_x = x; _y = y;
	LCD_comm(Y_C[y-1]+x-1); 
}


void LCD_showmsg(char y)
{
	char x;
	LCD_yx(y,1);
	for(x=0; x<X_NUM; x++) LCD_data(LCD_msg[_y-1][x]);
}

void LCD_clr(char my)
{
	char x,y;

	if (my==0){
		x_blink=0;
		y_blink=0;
		CURSOR_OFF;
		LCD_comm(0x1);
		for (y=0; y<Y_NUM; y++) {
			for(x=0; x<X_NUM; x++) LCD_msg[y][x]=' ';
		}
		LCD_yx(1,1);
	}
	else if (my<=Y_NUM) { // 1,2,3,4
	    y = my-1;
		for(x=0; x<X_NUM; x++) LCD_msg[y][x]=' ';
		LCD_showmsg(y+1);
		LCD_yx(my,1);
	}
}

#define LC_CLR_01     (0x01)
#define LC_XBLINK_02  (0x02)
#define LC_X_DEL_03   (0x03)
#define LC_X_ADD_4    (0x04)
#define LC_X0_05      (0x05)
#define LC_YBLINK_06  (0x06)
#define LC_SOUND1_07  (0x07)
#define LC_X_BACK_08  (0x08)

void LCD_putch(char c)
{
    LCD_msg[_y-1][_x-1]=c;
	LCD_data(c);
}	

void LCD_str(char y,char x,char *msg)
{
	unsigned char i,datas;
	
	if ((x<1)||(x>X_NUM)) x=_x;
	if ((y<1)||(y>Y_NUM)) y=_y;

	x--; y--;
	if (x>=X_NUM) x=0;
	if (y>=Y_NUM) y=0;

	i = 0; 
	while(1) {
		if ((datas=msg[i++])==0x00) break;
		if (datas==0xff) { i++; continue; }
	    if (datas==0x0d) {
				while(x<X_NUM) LCD_msg[y][x++] = ' ';
				LCD_showmsg(y+1);
				y++;
				if(y>=Y_NUM) y = 0;
				x = 0;
		}
		else if (datas==0x0a) ;
		else if (datas==LC_CLR_01) { LCD_clr(0); x=y=0;	}
		else if (datas==LC_XBLINK_02) x_blink=1;
		else if (datas==LC_X_DEL_03) { if (x>0) x--; }
		else if (datas==LC_X_ADD_4) { if (x<X_NUM-1) x++; }
		else if (datas==LC_X0_05) x=0; 
        else if (datas==LC_YBLINK_06) y_blink=y+1;
		else if (datas==LC_X_BACK_08) {  // backspace
				if (x>0) x--;
				LCD_msg[y][x]=' ';
		}
		else if ((datas>=Y_C[0])&&(datas<=Y_C[0]+X_NUM)) {
		         LCD_showmsg(y);
			     y=0; x=(datas-Y_C[0]); 
				 LCD_yx(y+1,x+1);
		}
		else if ((datas>=Y_C[1])&&(datas<=Y_C[1]+X_NUM)) {
		         LCD_showmsg(y+1);
			     y=1; x=(datas-Y_C[0]); 
				 LCD_yx(y+1,x+1);
		}
		else {
 		         //LCD_msg[y][x++] = datas;
 		         if (x>=X_NUM) {
			         LCD_showmsg(y+1);
		             //	if ((msg[i]!=0x0d)&&(msg[i]!=0x0a)) {
			         y++;
			         if(y>=Y_NUM) y = 0;
				     x=0;
		            //	}
		         }
	             LCD_msg[y][x++] = datas;
		}
	}
	LCD_showmsg(y+1);
	LCD_yx(y+1,x+1);
}

#define X_BLINK_MARK  (0x16)
unsigned char blinks;
void blink_pro(void)
{
    unsigned char i,y,x,oc;

	x=_x-1;
	y=_y-1;
	blink_dly=0;
    blinks=!blinks;
	if (x_blink&&(x<X_NUM)&&(y<Y_NUM)) {
        oc=LCD_msg[y][x];
        if (blinks) LCD_msg[y][x]=X_BLINK_MARK;
	    LCD_comm(Y_C[y]); 
	    for (i=0; i<X_NUM; i++)	LCD_data(LCD_msg[y][i]);
        LCD_msg[y][x]=oc;
	}
	if (y_blink&&(y_blink<=Y_NUM)) {
	    y=y_blink-1;
		LCD_comm(Y_C[y]);
        if (blinks) { for (i=0; i<X_NUM; i++) LCD_data(' '); }
	    else {  for (i=0; i<X_NUM; i++)	LCD_data(LCD_msg[y][i]); }
	}
}

LCD_FOCUS(U8 y,U8 x) 
{
	LCD_yx(y,x);
    CURSOR_BLINK; 
}

////////////////////////////////////////////////////
#define ES_PNUM  6
char edit_str_dec(char *s,int *p)
{
    int i=0,j;
	for (j=0; j<ES_PNUM; j++) {
       if (getint(s,&i,p+j)!=1)   break;
    }
	return(j); 
}

char edit_str_hex(char *s,int *p)
{
    unsigned char i=0,j;
	for (j=0; j<ES_PNUM; j++) {
       if (gethex(s,&i,p+j)!=1) break; 
    }
	return(j); 
}


unsigned char lcd_edit=0;
unsigned char lcd_edit_bit[2][2];
unsigned char editY,editX;

void lcd_edit_next_yx(void)
{
     unsigned char yx,n;
	 yx=(editY*12+editX+1)%24;
	 for (n=yx; n<24; n++) {
	     if (Ibit(lcd_edit_bit[n/12],n%12)) { editY=n/12; editX=n%12; return;}
	 }
	 for (n=0; n<yx; n++) {
	     if (Ibit(lcd_edit_bit[n/12],n%12)) { editY=n/12; editX=n%12; return;}
	 }
}

#define LCD_EDIT_M09   1
#define LCD_EDIT_M0F   2
#define LCD_EDIT_M01   3
#define LCD_EDIT_NUL   4
#define LCD_EDIT_X     5
	 	 
void lcd_edit_set(unsigned char editm,char *es1,short e1b,char *es2,short e2b)
{
     lcd_edit=editm;
	 LCD_clr(0);
     LCD_str(1,1,es1);
	 LCD_str(2,1,es2);
	 lcd_edit_bit[0][0]=e1b&0xff;
	 lcd_edit_bit[0][1]=(e1b>>8)&0x0f;
	 lcd_edit_bit[1][0]=e2b&0xff;
	 lcd_edit_bit[1][1]=(e2b>>8)&0x0f;
	 editY=1; editX=11;
	 lcd_edit_next_yx();
	 if (lcd_edit!=LCD_EDIT_NUL) LCD_FOCUS(editY+1,editX+1);
}

unsigned char lcd_edit_code_chk(unsigned char editm,unsigned char c)
{
     if (editm==LCD_EDIT_M09) {  // c='0'~'9'
	    if (c<'0') return('9');
		else if (c>'9') return('0');
		else return(c);
	 }
	 else if (editm==LCD_EDIT_M0F) {  // c='0'~'9','A'~'F'
	    if (c<'0') return('F');
		else if (c>'F') return('0');
		else if ((c>'9')&&(c<'A')) {
		    if (c==('9'+1)) return('A');
			else if (c==('A'-1)) return('9');
			else return('9');
		}
		else return(c);
	 }
	 if (editm==LCD_EDIT_M01) {  // c='0','1'
	    if (c<'0') return('1');
		else if (c>'1') return('0');
		else return(c);
	 }
	 
	 if (editm==LCD_EDIT_NUL) return(0);
	 if (editm==LCD_EDIT_X)   return(0);
	 return(c);
}


unsigned char lcd_edit_pro(void)
{
     U8  rc,c,i;
	 
     if ((rc=get_key())<=0) return(0);
     switch(rc) {
	     case KEY_ENTER : 
       case KEY_ESC   : lcd_edit=0; break;
	 	   case KEY_RIGHT : lcd_edit_next_yx(); 
		     if (lcd_edit!=LCD_EDIT_NUL) LCD_FOCUS(editY+1,editX+1);
		   break;
       
       case KEY_DOWN  : c=LCD_msg[editY][editX];
		     c=lcd_edit_code_chk(lcd_edit,c+1);
				 if (c==0) break;
				 LCD_msg[editY][editX]=c;
				 LCD_comm(Y_C[editY]);
	       for (i=0; i<X_NUM; i++) LCD_data(LCD_msg[editY][i]);
				 LCD_FOCUS(editY+1,editX+1);
		   break;
     }
	 return(rc);
}


//////////////////////////////////////////////////////

#define MAIN_STEP  0x1
#define VER_STEP   0x9
int  lcd_step=-1;

char lcd_ms_stus;

void lcd_ver_str(void)
{
    char s[10]="";
    if (file_error) sprintf(s,"E");		
#ifdef MASTER_SLAVE	  
	if (ms_stus==SW_MASTER) sprintf(dmsg,"%-9s#M%s\r",VER_MKS,s);
	else sprintf(dmsg,"%-9s#S%s\r",VER_MKS,s);
	lcd_ms_stus=ms_stus;
#else
    sprintf(dmsg,"%-10s %s\r",VER_MKS,s);
#endif	
	LCD_str(1,1,dmsg);
		
	sprintf(dmsg,"V%d.%d  %02d%02d%02d\r",VER_H,VER_L,VER_YY%100,VER_MN,VER_DD);
	LCD_str(2,1,dmsg);	
}

void lcd_ver(unsigned char real) 
{	
  
    if (!real) {	
#ifdef MASTER_SLAVE	 	
	   if (lcd_ms_stus==ms_stus) 
#endif
       return;	   
	}
	lcd_dly=0;
	LCD_clr(0);
    lcd_ver_str();
	lcd_step=VER_STEP;
}

void lcd_main_str(void)
{
    char s[10]=" ";
    
	if (file_error) sprintf(s,"E");		
#ifdef MASTER_SLAVE	  
	if (ms_stus==SW_MASTER) sprintf(dmsg,"MID:%03d%s %03d\r",mrs_id,s,N_MyIP[0][0]&0xff);
	else sprintf(dmsg,"SID:%03d%s %03d\r",mrs_id,s,N_MyIP[0][0]&0xff);
	lcd_ms_stus=ms_stus;
#else
    //ID:000 E 192.168.001.242
    sprintf(dmsg,"ID:%03d %s %03d\r",mrs_id,s,N_MyIP[0][0]&0xff);
#endif	

	LCD_str(1,1,dmsg);
	sprintf(dmsg,".%03d.%03d.%03d\r",N_MyIP[0][0]>>8,N_MyIP[0][1]&0xff,N_MyIP[0][1]>>8);
	LCD_str(2,1,dmsg);  	
}
void lcd_main(unsigned char real) 
{	
    if (!real) {
#ifdef MASTER_SLAVE	 	
	   if (lcd_ms_stus==ms_stus) 
#endif		
       return;
	}
	LCD_clr(0);
    lcd_main_str();
	lcd_step=MAIN_STEP;
}


#define MMENU_NUM    4
#define MMENU_STEP 0x2
int  lcd_mmenu_idx;
void lcd_mmenu(int real)
{
     int  y,idx;
     
	 if (!real) {
         //if (lcd_dly<1000) 
		 return;
	 }
	 lcd_dly=0;
 
	 if (lcd_mmenu_idx<0) lcd_mmenu_idx=MMENU_NUM-1;
	 else if (lcd_mmenu_idx>=MMENU_NUM) lcd_mmenu_idx=0;
	 LCD_clr(0);
	 idx=lcd_mmenu_idx;
	 for (y=1; y<=2; y++,idx++) {
	     switch(idx) {
		     default :
			 case 0  : idx=0;
		               sprintf(dmsg,"%d.SYS   Info\r",idx+1); break;
			 case 1  : sprintf(dmsg,"%d.DATA  Info\r",idx+1); break;
			 case 2  : sprintf(dmsg,"%d.SMB   Info\r",idx+1); break;			 
			 case 3  : sprintf(dmsg,"%d.CONFIG \r",idx+1); break;
		 }
		 LCD_str(y,1,dmsg);	
	 }
	 LCD_FOCUS(1,1);
	 lcd_step=MMENU_STEP;
}

///////////////////////////////////////
#define LCD_MSTUS_STEP      0x10
#define LCD_MSTUS_SET_STEP  0x11
int  lcd_mstus_idx;
char lcd_mstus_hc;
int  lcd_mstus_ch;
void lcd_mstus(int real)
{
     int   idx,max,n,ch,SYS;
	 char  hc,name[30];
     
	 if (!real) {
         if (lcd_dly<1000) return;
	 }
	 lcd_dly=0;
     n=0;
LS:	 
     SYS=3;
	 max=SYS+SS_AI_NUM+SS_DI_NUM+SS_DO_NUM;
	 if (++n>=max) return;
	 
	 if (lcd_mstus_idx<0) lcd_mstus_idx=max-1;
	 else if (lcd_mstus_idx>=max) lcd_mstus_idx=0;
	 
	 idx=lcd_mstus_idx;
	 if (idx<SYS) { hc='s'; ch=idx; }
     else if (idx<(SYS+SS_AI_NUM)) { hc='a'; ch=idx-SYS; }
	 else if (idx<(SYS+SS_AI_NUM+SS_DI_NUM))  { hc='i'; ch=idx-(SYS+SS_AI_NUM); }
	 else  { hc='o'; ch=idx-(SYS+SS_AI_NUM+SS_DI_NUM); }

     if (hc=='s') {
	     LCD_clr(0);
	     switch(ch) {
		    case 0 : lcd_ver_str();  break;
		    case 1 : lcd_main_str(); break;
		    case 2 : strncpy(name,sys_site,10);
			         sprintf(dmsg,"S:%s\r",name); 
					 LCD_str(1,1,dmsg);
					 strncpy(name,sys_location,10);
			         sprintf(dmsg,"L:%s\r",name); 			       
					 LCD_str(2,1,dmsg);
					 break;
		 }
	 }
	 else {
         lcd_mstus_hc=hc;
         lcd_mstus_ch=ch;
     
	     sprintf(name,"%s",name_str(hc,ch)); name[12]=0x0;
	     if (strlen(name)<=0)  {
	         if (key_rc==KEY_RIGHT) lcd_mstus_idx--; 
             else lcd_mstus_idx++; 
		     goto LS;
	     }
	 
	     LCD_clr(0);
	     sprintf(dmsg,"%s\r",name); 
	     LCD_str(1,1,dmsg);
	 
	     if (hc=='a') sprintf(dmsg,"AI%03d:%6d\r",ch,(S16)AI_get_ibuf(ch)); 	
	     else if (hc=='i') sprintf(dmsg,"DI%03d: %d\r",ch,IO_stus(hc,ch)); 
	     else if (hc=='o') sprintf(dmsg,"DO%03d: %d\r",ch,IO_stus(hc,ch)); 
	     LCD_str(2,1,dmsg);	
	 
	     if ((hc=='a')||(hc=='o')) LCD_FOCUS(1,1);
	 }
	 lcd_step=LCD_MSTUS_STEP;
}

int lcd_mstus_dstr(S8 *es1,S8 *es2,U16 *e1,U16 *e2,int *em)
{
	 int   ch;
	 char  hc,name[30];

     hc=lcd_mstus_hc;
     ch=lcd_mstus_ch;

	 sprintf(name,"%s",name_str(hc,ch)); name[12]=0x0;
	 if (strlen(name)<=0)  return(0);
	 
	 sprintf(es1,"%s\r",name);
	 if (hc=='a') {
		  sprintf(es2,"Set : %05d\r",(S16)AI_get_ibuf(ch));
		  *e1=0x0000; *e2=0x07C0; 
		  *em=LCD_EDIT_M09;
		  return(1);
	 }
	 else if (hc=='o') {
		  sprintf(es2,"Ctrl:0 1 010\r");
		  *e1=0x0000; *e2=0x04B0; 
		  *em=LCD_EDIT_X;
		  return(1);
	 }
	 return(0);
}
			  
void lcd_mstus_set(void)
{
     S8   es1[20],es2[20];
	 U16  e1,e2;
	 int  em;

	 lcd_dly=0;
	 if (lcd_mstus_dstr(es1,es2,&e1,&e2,&em)!=1) return;
	 lcd_edit_set(em,es1,e1,es2,e2); 
	 lcd_step=LCD_MSTUS_SET_STEP;
}

////////////////////////////////////////////
#define LCD_SYS_NUM    10
#define LCD_SYS_STEP 0x100
int  lcd_sys_idx;
void lcd_sys(int real)
{
     int  idx;
     if (!real) {
         if (lcd_dly<1000) return;
	 }
	 lcd_dly=0;
     LCD_clr(0);
	 
	 
	 if (lcd_sys_idx<0) lcd_sys_idx=LCD_SYS_NUM-1;
	 else if (lcd_sys_idx>=LCD_SYS_NUM) lcd_sys_idx=0;
	 idx=lcd_sys_idx;
	 switch(idx) {
	     default :
		 case 0  : 
		      idx=lcd_sys_idx=0;
			  lcd_ver_str();
		      break;
			  
		 case 1 :
              sprintf(dmsg," %04d/%02d/%02d\r",Tyy,Tmn,Tdd);
	          LCD_str(1,1,dmsg);
	          sprintf(dmsg,"  %02d:%02d:%02d\r",Thh,Tmm,Tss);
	          LCD_str(2,1,dmsg);
			  break;			 
	     case 2 : 
		      sprintf(dmsg,"IP       %03d\r",N_MyIP[0][0]&0xff);
			  LCD_str(1,1,dmsg);
			  sprintf(dmsg,".%03d.%03d.%03d\r",N_MyIP[0][0]>>8,N_MyIP[0][1]&0xff,N_MyIP[0][1]>>8);
			  LCD_str(2,1,dmsg);   
		      break;
	     case 3 : 
		      sprintf(dmsg,"NetMask  %03d\r",N_SubnetMask[0][0]&0xff);
			  LCD_str(1,1,dmsg);
			  sprintf(dmsg,".%03d.%03d.%03d\r",N_SubnetMask[0][0]>>8,N_SubnetMask[0][1]&0xff,N_SubnetMask[0][1]>>8);
			  LCD_str(2,1,dmsg);   
		      break;
		 case 4 : 
		      sprintf(dmsg,"Gateway  %03d\r",N_GatewayIP[0][0]&0xff);
			  LCD_str(1,1,dmsg);
			  sprintf(dmsg,".%03d.%03d.%03d\r",N_GatewayIP[0][0]>>8,N_GatewayIP[0][1]&0xff,N_GatewayIP[0][1]>>8);
			  LCD_str(2,1,dmsg);   
		      break;					  	
			  	  
	     case 5 : 
		      sprintf(dmsg,"IP2      %03d\r",N_MyIP[1][0]&0xff);
			  LCD_str(1,1,dmsg);
			  sprintf(dmsg,".%03d.%03d.%03d\r",N_MyIP[1][0]>>8,N_MyIP[1][1]&0xff,N_MyIP[1][1]>>8);
			  LCD_str(2,1,dmsg);   
		      break;
	     case 6 : 
		      sprintf(dmsg,"NetMask2 %03d\r",N_SubnetMask[1][0]&0xff);
			  LCD_str(1,1,dmsg);
			  sprintf(dmsg,".%03d.%03d.%03d\r",N_SubnetMask[1][0]>>8,N_SubnetMask[1][1]&0xff,N_SubnetMask[1][1]>>8);
			  LCD_str(2,1,dmsg);   
		      break;
		 case 7 : 
		      sprintf(dmsg,"Gateway2 %03d\r",N_GatewayIP[1][0]&0xff);
			  LCD_str(1,1,dmsg);
			  sprintf(dmsg,".%03d.%03d.%03d\r",N_GatewayIP[1][0]>>8,N_GatewayIP[1][1]&0xff,N_GatewayIP[1][1]>>8);
			  LCD_str(2,1,dmsg);   
		      break;
		
	     case 8 : 
		      sprintf(dmsg,"MAC   %02X.%02X\r",MyMAC[0],MyMAC[1]);
			  LCD_str(1,1,dmsg);
			  sprintf(dmsg,"%02X.%02X.%02X.%02X\r",MyMAC[2],MyMAC[3],MyMAC[4],MyMAC[5]);
			  LCD_str(2,1,dmsg);   
		      break;		
         case 9 : 
		      sprintf(dmsg,"S2=%02X S3=%02X\r",sw2,sw3); 
			  LCD_str(1,1,dmsg);
#ifdef PLC_FUN 			  
	          if (!sw_plc_run) sprintf(dmsg,"PLC: SW STOP\r"); 
	          else {
	             switch(plc_runf) {
	                 case PLC_RUNF_RUN  : sprintf(dmsg,"PLC: RUNNING\r"); break;
		             case PLC_RUNF_FAIL : sprintf(dmsg,"PLC: FAIL\r"); break;
	                 case PLC_RUNF_INIT : sprintf(dmsg,"PLC: INIT.\r"); break;
	                 case PLC_RUNF_STOP : sprintf(dmsg,"PLC: STOP\r"); break;
		             default : sprintf(dmsg,"PLC: unknown\r"); break;
	             }
	         }	 		 
#endif
			 LCD_str(2,1,dmsg);			 
		     break;			  
	 }
	 lcd_step=LCD_SYS_STEP;
}

////////////////////////////////////////////

#define LCD_DATA_STEP 0x200
#define LCD_DATA_PAGE 3
int  lcd_data_idx,lcd_ss_lno;
void lcd_data(int real)
{
  int  n,lno,max;
  SS_PARM  *pss;
	 
  if (!real) {
    if (lcd_dly<1000) return;
  }
  lcd_dly=0;	 
  max=SS_NUM*LCD_DATA_PAGE;
  if (lcd_data_idx<0) lcd_data_idx=max-1;
  else if (lcd_data_idx>=max) lcd_data_idx=0;
  LCD_clr(0);
  lcd_ss_lno=lno=lcd_data_idx/LCD_DATA_PAGE;
  n=lcd_data_idx%LCD_DATA_PAGE;
  pss=(SS_PARM *)&SS[lno];
	 
  sprintf(dmsg,"Module ID=%d\r",lno);
  LCD_str(1,1,dmsg);	
  switch(n) {
    case 0 : sprintf(dmsg,"DI NUM=%d\r",pss->DIN); break;
    case 1 : sprintf(dmsg,"DO NUM=%d\r",pss->DON); break;
    case 2 : sprintf(dmsg,"AI NUM=%d\r",pss->AIN); break;     		         
  }
  LCD_str(2,1,dmsg);
  LCD_FOCUS(1,1);
  lcd_step=LCD_DATA_STEP;
}

/////////////////////
#define LCD_DATA_DI_STEP (LCD_DATA_STEP+0x10)
int  lcd_data_di_idx;
void lcd_data_di(int real)
{
     int  cno,lno,max,bno;
	 SS_PARM  *pss;
	 
     if (!real) {
         if (lcd_dly<1000) return;
	 }
	 lcd_dly=0;	 
	 LCD_clr(0);
	 
	 max=SS_DI_NUM;
	 if (lcd_data_di_idx<0) lcd_data_di_idx=max-8;
	 else if (lcd_data_di_idx>=max) lcd_data_di_idx=0;
	 lcd_data_di_idx=(lcd_data_di_idx+7)/8*8;
	 for (lno=0; lno<SS_NUM; lno++) {
         pss=(SS_PARM *)&SS[lno];
	 	 if ((lcd_data_di_idx>=pss->DIS)&&(lcd_data_di_idx<(pss->DIS+pss->DIN))) break;
	 }
	 cno=lcd_data_di_idx-pss->DIS;
	 bno=lcd_data_di_idx/8;
	 	 
	 sprintf(dmsg,"[%02d] %3d-%3d\r",lno,lcd_data_di_idx,cno);
	 LCD_str(1,1,dmsg);	         
     sprintf(dmsg,"DI %s\r",bin_str(DI_STUS[bno]));
	 LCD_str(2,1,dmsg);
	 //LCD_FOCUS(1,1);
	 lcd_step=LCD_DATA_DI_STEP;
}

/////////////////////
#define LCD_DATA_DO_STEP (LCD_DATA_STEP+0x20)
int  lcd_data_do_idx;
void lcd_data_do(int real)
{
     int  cno,lno,max,bno;
	 SS_PARM  *pss;
	 
     if (!real) {
         if (lcd_dly<1000) return;
	 }
	 lcd_dly=0;	 
	 LCD_clr(0);
	 
	 max=SS_DO_NUM;
	 if (lcd_data_do_idx<0) lcd_data_do_idx=max-8;
	 else if (lcd_data_do_idx>=max) lcd_data_do_idx=0;
	 lcd_data_do_idx=(lcd_data_do_idx+7)/8*8;
	 for (lno=0; lno<SS_NUM; lno++) {
         pss=(SS_PARM *)&SS[lno];
	 	 if ((lcd_data_do_idx>=pss->DOS)&&(lcd_data_do_idx<(pss->DOS+pss->DON))) break;
	 }
	 cno=lcd_data_do_idx-pss->DOS;
	 bno=lcd_data_do_idx/8;
	 	 
	 sprintf(dmsg,"[%02d] %3d-%3d\r",lno,lcd_data_do_idx,cno);
	 LCD_str(1,1,dmsg);	         
     sprintf(dmsg,"DO %s\r",bin_str(DO_STUS[bno]));
	 LCD_str(2,1,dmsg);
	 //LCD_FOCUS(1,1);
	 lcd_step=LCD_DATA_DO_STEP;
}

/////////////////////
#define LCD_DATA_AI_STEP (LCD_DATA_STEP+0x30)
int  lcd_data_ai_idx;
void lcd_data_ai(int real)
{
     int  ch,cno,lno,max;
	 SS_PARM  *pss;
	 
     if (!real) {
         if (lcd_dly<1000) return;
	 }
	 lcd_dly=0;	 
	 LCD_clr(0);
	 
	 max=SS_AI_NUM;
	 if (lcd_data_ai_idx<0) lcd_data_ai_idx=max-1;
	 else if (lcd_data_ai_idx>=max) lcd_data_ai_idx=0;
	 
	 for (lno=0; lno<SS_NUM; lno++) {
         pss=(SS_PARM *)&SS[lno];
	 	 if ((lcd_data_ai_idx>=pss->AIS)&&(lcd_data_ai_idx<(pss->AIS+pss->AIN))) break;
	 }
	 ch=lcd_data_ai_idx;
	 cno=lcd_data_ai_idx-pss->AIS;
	 	 
	 sprintf(dmsg,"[%02d] %3d-%d\r",lno,ch,cno);
	 LCD_str(1,1,dmsg);	 
	 sprintf(dmsg,"AI %d\r",(S16)AI_get_ibuf(ch));
	 LCD_str(2,1,dmsg);
	 //LCD_FOCUS(1,1);
	 lcd_step=LCD_DATA_AI_STEP;
}

////////////////////////////////////////////
#define LCD_MRS_STEP 0x300
int  lcd_mrs_idx;
void lcd_mrs(int real)
{
     int  lno,idx,max=0; 

     if (!real) {
         if (lcd_dly<1000) return;
	 }
	 lcd_dly=0;
	 max=MRS_NUM+1;	 
	 if (lcd_mrs_idx<0) lcd_mrs_idx=max-1;
	 else if (lcd_mrs_idx>=max) lcd_mrs_idx=0;
	 LCD_clr(0);
	 idx=lcd_mrs_idx;
	 switch(idx) {
	     case 0  : 
             sprintf(dmsg,"Slave Modbus\r");  		 
			 LCD_str(1,1,dmsg);	
			 sprintf(dmsg,"ID=%d\r",mrs_id); 
			 LCD_str(2,1,dmsg);	
			 break;
		 default :  
			 lno=idx-1;
			 if (MRS[lno].portno<UART_NUM) {
			     sprintf(dmsg,"P#%d COM\r",lno+1);  
			     LCD_str(1,1,dmsg);	
			     sprintf(dmsg,"PORT %5d\r",MRS[lno].portno); 
				 LCD_str(2,1,dmsg);					 
			 }
			 else if (MRS[lno].portno<65535) {
                 if (MRS[lno].protocol==PROT_UDP)   sprintf(dmsg,"P#%d NET UDP\r",lno+1); 
		         else if (MRS[lno].protocol==PROT_TCP) sprintf(dmsg,"P#%d NET TCP\r",lno+1); 
		   		 else sprintf(dmsg,"P#%d NET ???\r",lno+1);  
			     LCD_str(1,1,dmsg);	
			     sprintf(dmsg,"PORT %5d\r",MRS[lno].portno); 
				 LCD_str(2,1,dmsg);
			 } 	
			 else {
			     sprintf(dmsg,"P#%d Unknow\r",lno+1);  
			     LCD_str(1,1,dmsg);	
			     sprintf(dmsg,"PORT %5d\r",MRS[lno].portno); 
				 LCD_str(2,1,dmsg);
			 }
		     break;
	 }
	 //LCD_FOCUS(1,1);
	 lcd_step=LCD_MRS_STEP;
}

/////////////////////////////////////////////////////////////////////////////////
#define LCD_CFG_STEP      0x400
#define LCD_CFG_SET_STEP  (LCD_CFG_STEP+0x10)
#define CFG_NOR_IDX       20
int  lcd_cfg_idx;
void lcd_cfg(int real)
{
     int  idx,max;
	 int  lno,protocol;

     if (!real) {
         if (lcd_dly<1000) return;
	 }
	 lcd_dly=0;
	 LCD_clr(0);	 
	 max=CFG_NOR_IDX;
LS:	 	 
	 if (lcd_cfg_idx<0) lcd_cfg_idx=max-1;
	 else if (lcd_cfg_idx>=max) lcd_cfg_idx=0;	 
	 idx=lcd_cfg_idx;
	 switch(idx) {
		 case 0  : 
		      sprintf(dmsg,"IP       %03d\r",N_MyIP[0][0]&0xff);
			  LCD_str(1,1,dmsg);
			  sprintf(dmsg,".%03d.%03d.%03d\r",N_MyIP[0][0]>>8,N_MyIP[0][1]&0xff,N_MyIP[0][1]>>8);
			  LCD_str(2,1,dmsg);   
		      break;
	     case 1 : 
		      sprintf(dmsg,"NetMask  %03d\r",N_SubnetMask[0][0]&0xff);
			  LCD_str(1,1,dmsg);
			  sprintf(dmsg,".%03d.%03d.%03d\r",N_SubnetMask[0][0]>>8,N_SubnetMask[0][1]&0xff,N_SubnetMask[0][1]>>8);
			  LCD_str(2,1,dmsg);   
		      break;
		 case 2 : 
		      sprintf(dmsg,"Gateway  %03d\r",N_GatewayIP[0][0]&0xff);
			  LCD_str(1,1,dmsg);
			  sprintf(dmsg,".%03d.%03d.%03d\r",N_GatewayIP[0][0]>>8,N_GatewayIP[0][1]&0xff,N_GatewayIP[0][1]>>8);
			  LCD_str(2,1,dmsg);   
		      break;
			  
		 case 3  : 
		      sprintf(dmsg,"IP2      %03d\r",N_MyIP[1][0]&0xff);
			  LCD_str(1,1,dmsg);
			  sprintf(dmsg,".%03d.%03d.%03d\r",N_MyIP[1][0]>>8,N_MyIP[1][1]&0xff,N_MyIP[1][1]>>8);
			  LCD_str(2,1,dmsg);   
		      break;
	     case 4 : 
		      sprintf(dmsg,"NetMask2 %03d\r",N_SubnetMask[1][0]&0xff);
			  LCD_str(1,1,dmsg);
			  sprintf(dmsg,".%03d.%03d.%03d\r",N_SubnetMask[1][0]>>8,N_SubnetMask[1][1]&0xff,N_SubnetMask[1][1]>>8);
			  LCD_str(2,1,dmsg);   
		      break;
		 case 5 : 
		      sprintf(dmsg,"Gateway2 %03d\r",N_GatewayIP[1][0]&0xff);
			  LCD_str(1,1,dmsg);
			  sprintf(dmsg,".%03d.%03d.%03d\r",N_GatewayIP[1][0]>>8,N_GatewayIP[1][1]&0xff,N_GatewayIP[1][1]>>8);
			  LCD_str(2,1,dmsg);   
		      break; 	 	 
         case 6 :
		 case 7 :
		      lno=lcd_cfg_idx-6;
			  if (MRS[lno].protocol==PROT_TCP) protocol=1;
			  else protocol=0;  // UDP
			  
		      sprintf(dmsg,"P%d SMB T/U=%d\r",lno+1,protocol); 
			  LCD_str(1,1,dmsg);	 
			  sprintf(dmsg,"PORTNO=%05d\r",MRS[lno].portno); 
			  LCD_str(2,1,dmsg);	
			  break;
	     case 8 : 
		      sprintf(dmsg,"eBoot\r");
			  LCD_str(1,1,dmsg);
			  sprintf(dmsg,"\r");
			  LCD_str(2,1,dmsg);   
			  LCD_FOCUS(1,1);
		      break;						  
         case 9 : 
		      sprintf(dmsg,"Loader\r");
			  LCD_str(1,1,dmsg);
			  sprintf(dmsg,"%s\r",RUN_FILE);
			  LCD_str(2,1,dmsg);   
		      break;			  			  
		 case 10: 
		      sprintf(dmsg,"Loader\r");
			  LCD_str(1,1,dmsg);
			  sprintf(dmsg,"%s\r",PLC_FILE);
			  LCD_str(2,1,dmsg);   
		      break;		
		 case 11: 
		      sprintf(dmsg,"Loader\r");
			  LCD_str(1,1,dmsg);
			  sprintf(dmsg,"%s\r",HEX_FILE);
			  LCD_str(2,1,dmsg);   
		      break;
#ifdef SDNP3			  	  
		 case 12:
		      sprintf(dmsg,"SDNP3 REMOTE\r");
			  LCD_str(1,1,dmsg);
			  sprintf(dmsg,"ADDR=%05d\r",plgn[0]->LADDR);
			  LCD_str(2,1,dmsg); 
			  break;
		 case 13:
		      sprintf(dmsg,"SDNP3 LOCAL\r");
			  LCD_str(1,1,dmsg);
			  sprintf(dmsg,"ADDR=%05d\r",plgn[0]->RADDR);
			  LCD_str(2,1,dmsg); 
			  break;			  
         case 14:
		 case 15:
		      lno=lcd_cfg_idx-14;
			  if ((prsn[lno]->ptcpudp==PROT_TCP)||(prsn[lno]->ptcpudp==1)) protocol=1;  // TCP
			  else protocol=0;  // UDP			  
		      sprintf(dmsg,"P%d SD3 T/U=%d\r",lno+1,protocol); 
			  LCD_str(1,1,dmsg);	 
			  sprintf(dmsg,"PORTNO=%05d\r",prsn[lno]->portno); 
			  LCD_str(2,1,dmsg);	
			  break;			  			  
#endif
			  	   
		 default: lcd_cfg_idx++;
		          goto LS;
	 }
	 LCD_FOCUS(1,1);
	 lcd_step=LCD_CFG_STEP;
}

int lcd_cfg_dstr(S8 *es1,S8 *es2,U16 *e1,U16 *e2,int *em)
{
	 int  lno,protocol;
	 
	 switch(lcd_cfg_idx) {
	     case 0 : 
		      sprintf(es1,"IP       %03d\r",N_MyIP[0][0]&0xff);
			  sprintf(es2,".%03d.%03d.%03d\r",N_MyIP[0][0]>>8,N_MyIP[0][1]&0xff,N_MyIP[0][1]>>8);
			  *e1=0x0E00; *e2=0x0EEE; 
			  *em=LCD_EDIT_M09;
		      return(1);
	     case 1 : 
              sprintf(es1,"NetMask  %03d\r",N_SubnetMask[0][0]&0xff);
			  sprintf(es2,".%03d.%03d.%03d\r",N_SubnetMask[0][0]>>8,N_SubnetMask[0][1]&0xff,N_SubnetMask[0][1]>>8);
			  *e1=0x0E00; *e2=0x0EEE; 
			  *em=LCD_EDIT_M09;
		      return(1);
		 case 2 : 
              sprintf(es1,"Gateway  %03d\r",N_GatewayIP[0][0]&0xff);
			  sprintf(es2,".%03d.%03d.%03d\r",N_GatewayIP[0][0]>>8,N_GatewayIP[0][1]&0xff,N_GatewayIP[0][1]>>8);
			  *e1=0x0E00; *e2=0x0EEE; 
			  *em=LCD_EDIT_M09;
		      return(1);
			  
	     case 3 : 
		      sprintf(es1,"IP2      %03d\r",N_MyIP[1][0]&0xff);
			  sprintf(es2,".%03d.%03d.%03d\r",N_MyIP[1][0]>>8,N_MyIP[1][1]&0xff,N_MyIP[1][1]>>8);
			  *e1=0x0E00; *e2=0x0EEE; 
			  *em=LCD_EDIT_M09;
		      return(1);
	     case 4 : 
              sprintf(es1,"NetMask2 %03d\r",N_SubnetMask[1][0]&0xff);
			  sprintf(es2,".%03d.%03d.%03d\r",N_SubnetMask[1][0]>>8,N_SubnetMask[1][1]&0xff,N_SubnetMask[1][1]>>8);
			  *e1=0x0E00; *e2=0x0EEE; 
			  *em=LCD_EDIT_M09;
		      return(1);
		 case 5 : 
              sprintf(es1,"Gateway2 %03d\r",N_GatewayIP[1][0]&0xff);
			  sprintf(es2,".%03d.%03d.%03d\r",N_GatewayIP[1][0]>>8,N_GatewayIP[1][1]&0xff,N_GatewayIP[1][1]>>8);
			  *e1=0x0E00; *e2=0x0EEE; 
			  *em=LCD_EDIT_M09;
		      return(1);			  	 	 	  
         case 6 :
		 case 7 :
		      lno=lcd_cfg_idx-6;
			  if (MRS[lno].protocol==PROT_TCP) protocol=1;
			  else protocol=0;  // UDP
			  
		      sprintf(es1,"P%d SMB T/U=%d\r",lno+1,protocol); 
			  sprintf(es2,"PORTNO=%05d\r",MRS[lno].portno); 
	          *e1=0x0800; *e2=0x0F80; 
			  *em=LCD_EDIT_M09;
			  return(1);
         case  8:		 		 
		      sprintf(es1,"eBoot\r"); 			  
			  sprintf(es2,"Execute(Y/N)\r"); 
	          *e1=0x0000; *e2=0x0000; 
			  *em=LCD_EDIT_NUL;
			  return(1);			  
         case  9:		 		 
		      sprintf(es1,"%s\r",RUN_FILE); 			  
			  sprintf(es2,"Execute(Y/N)\r"); 
	          *e1=0x0000; *e2=0x0000; 
			  *em=LCD_EDIT_NUL;
			  return(1);
         case 10:		 		 
		      sprintf(es1,"%s\r",PLC_FILE); 			  
			  sprintf(es2,"Execute(Y/N)\r"); 
	          *e1=0x0000; *e2=0x0000; 
			  *em=LCD_EDIT_NUL;
			  return(1);
			  			  			  
		 case 11:		 		 
		      sprintf(es1,"%s\r",HEX_FILE); 			  
			  sprintf(es2,"Execute(Y/N)\r"); 
	          *e1=0x0000; *e2=0x0000; 
			  *em=LCD_EDIT_NUL;
			  return(1); 

#ifdef SDNP3			  	  
		 case 12:
		      sprintf(es1,"SDNP3 REMOTE\r");
			  sprintf(es2,"ADDR=%05d\r",plgn[0]->LADDR);
			  *e1=0x0000; *e2=0x03E0; 
			  *em=LCD_EDIT_M09;
			  return(1);
		 case 13:
		      sprintf(es1,"SDNP3 LOCAL\r");
			  sprintf(es2,"ADDR=%05d\r",plgn[0]->RADDR);
			  *e1=0x0000; *e2=0x03E0; 
			  *em=LCD_EDIT_M09;
			  return(1);
         case 14:
		 case 15:
		      lno=lcd_cfg_idx-14;
			  if ((prsn[lno]->ptcpudp==PROT_TCP)||(prsn[lno]->ptcpudp==1)) protocol=1;  // TCP
			  else protocol=0;  // UDP			  
		      sprintf(es1,"P%d SD3 T/U=%d\r",lno+1,protocol); 	 
			  sprintf(es2,"PORTNO=%05d\r",prsn[lno]->portno); 
	          *e1=0x0800; *e2=0x0F80; 
			  *em=LCD_EDIT_M09;
			  return(1);			  
#endif			  
	 }
	 return(0);
}
					   
void lcd_cfg_set(void)
{
     S8   es1[20],es2[20];
	 U16  e1,e2;
	 int  em;

	 lcd_dly=0;
	 if (lcd_cfg_dstr(es1,es2,&e1,&e2,&em)!=1) return;
	 lcd_edit_set(em,es1,e1,es2,e2); 
	 lcd_step=LCD_CFG_SET_STEP;
}
///////////////////////////////////////////////////////////////////
int     pwr_set=0;
int     pwr_run_step;
#define LCD_PWR_SET_STEP  0x900
void lcd_pwr_set(int run_step)
{
     S8   es1[20],es2[20];
	 U16  e1,e2;
	 int  em;

     sprintf(es1,"PASSWORD 000\r"); 	
	 sprintf(es2,"\r");  
	 e1=0x0E00; e2=0x0000; 
	 em=LCD_EDIT_M09;
				  
	 lcd_dly=0;
	 lcd_edit_set(em,es1,e1,es2,e2); 
	 lcd_step=LCD_PWR_SET_STEP;
	 pwr_run_step=run_step;
}


//////////////////////////////////////////////////////////////////////////////////
//#define LCD_RDY_CHK_PRO 1

#ifdef LCD_RDY_CHK_PRO
U8 lcd_same_key=0; 
void lcd_rdy_pro(void)
{
    char rc;
    if (!lcd_rdy) {
	    if ((rc=get_key())<=0) return;	   	
	    switch(lcd_same_key) {
		     default:
	         case 0 : if (rc==KEY_ENTER) lcd_same_key=1; else lcd_same_key=0;
		  	 		  break;
		     case 1 : if (rc==KEY_DOWN) lcd_same_key++; else lcd_same_key=0;  
		              break; 
		     case 2 : if (rc==KEY_RIGHT) lcd_same_key++; else lcd_same_key=0;
					  break;			 
		     case 3 : if (rc==KEY_DOWN) lcd_same_key++; else lcd_same_key=0;  
		              break;    
		     case 4 : if (rc==KEY_RIGHT) lcd_same_key++; else lcd_same_key=0;
					  break;			 		   
             case 5 : if (rc==KEY_ENTER) { lcd_rdy=1;  lcd_step=0;  LCD_LIGHT(1); }
					  lcd_same_key=0;  
					  break;			 				 
	    }			 
	    return;
	}	
}
#endif

void sm_data_set(void);
void dbg_cdo_pro(char *s_ch,char *s_stus);
void dbg_wao_pro(char *s_ch,char *s_var);

void lcd_pro(void)
{
  char  rc=0,ret;
	int   ep[ES_PNUM],lno;
	char  dstr[20],cstr[20];

#ifdef LCD_RDY_CHK_PRO	
	if (!lcd_rdy)  lcd_rdy_pro();
#endif
	if (!lcd_rdy) { lcd_sec=0; return; }
				
  if (lcd_sec) {
    LcdLightOffSec+=lcd_sec;
    lcd_sec=0;
	  if (LcdLightOffSec>300) {
      pwr_set=0;
      LcdLightOffSec=0;
      LCD_LIGHT(0);
	    if (lcd_step!=MAIN_STEP) lcd_step=0;
	    lcd_edit=0;	
#ifdef LCD_RDY_CHK_PRO		   
		  lcd_rdy=0;  
		  LCD_clr(0); lcd_step=VER_STEP;
		  return;
#endif
	  }
  }
	
  if (lcd_edit) {
    rc=lcd_edit_pro();
		switch(rc) {
	    case KEY_ENTER : 
      case KEY_ESC   : lcd_edit=0; break;
		  default        : return;
    }
	}
 //   if ((y_blink||x_blink)&&(blink_dly>=500)) blink_pro(); 
 
  switch(lcd_step) {
    case -1 : lcd_ver(1); break;
    case 0  : lcd_edit=0; lcd_main(1); break;

		case VER_STEP :
      if (lcd_dly<3000) return;
			lcd_edit=0;  lcd_main(1);
			break;
			
    case MAIN_STEP : 
      lcd_main(0);
      if ((rc=get_key())<=0) return;
      switch(rc) {
	      case KEY_DOWN :  
		    // lcd_mstus_idx=0;
		    lcd_mstus(1);  break;
		     
        default : 
		      lcd_mmenu_idx=0;
		      lcd_mmenu(1); break;
      }
    break; 
			
		case MMENU_STEP : 
      lcd_mmenu(0);
      if ((rc=get_key())<=0) return;
			lcd_edit=0;
      switch(rc) {
  	    case KEY_ENTER : 
          switch(lcd_mmenu_idx) {
            case 0 : lcd_sys_idx=0;  lcd_sys(1); break;
            case 1 : lcd_data_idx=0; lcd_data(1); break;
					  case 2 : lcd_mrs_idx=0;  lcd_mrs(1);  break;
					  case 3 : lcd_cfg_idx=0;
              if (!pwr_set) lcd_pwr_set(LCD_CFG_STEP);
						  else lcd_cfg(1);
					  break;
				  }				
        break;
        
        case KEY_ESC   : lcd_main(1); break;
				case KEY_RIGHT : lcd_mmenu_idx--; lcd_mmenu(1); break;
        case KEY_DOWN  : lcd_mmenu_idx++; lcd_mmenu(1); break;
      }
    break; 

    case LCD_MSTUS_STEP : 
		  lcd_mstus(0);
      if ((rc=get_key())<=0) return;
      switch(rc) {
				case KEY_ENTER : lcd_mstus_set(); break;
        case KEY_ESC   : lcd_main(1); break;
				case KEY_RIGHT : lcd_mstus_idx--; lcd_mstus(1); break;
        case KEY_DOWN  : lcd_mstus_idx++; lcd_mstus(1); break;
      }
    break; 			

    case LCD_MSTUS_SET_STEP:
      switch(rc) {
			  case KEY_ENTER : 
				  LCD_msg[0][X_NUM]=0; LCD_msg[1][X_NUM]=0; 
					sprintf(cstr,"%s",ch2str(lcd_mstus_hc,lcd_mstus_ch));
					if (lcd_mstus_hc=='a') {
					  sprintf(dstr,"%s",&LCD_msg[1][6]);
					  dbg_wao_pro(cstr,dstr);
					} else if (lcd_mstus_hc=='o') {
					    if (editX==5) sprintf(dstr,"0");
						  else if (editX==7)  sprintf(dstr,"1");
						  else if (editX==10) sprintf(dstr,"2");
						  else break;
					    dbg_cdo_pro(cstr,dstr);
					}
        break;			
		  }
			lcd_mstus(1); 
		break;
						
////////////////////////////////////////////////////////////////					
    case LCD_SYS_STEP:
      lcd_sys(0);
      if ((rc=get_key())<=0) return;
      switch(rc) {
        case KEY_ENTER : lcd_sys(1); break;
        case KEY_ESC   : lcd_mmenu(1); break;
        case KEY_RIGHT : lcd_sys_idx--; lcd_sys(1); break;
        case KEY_DOWN  : lcd_sys_idx++; lcd_sys(1); break;
      }
    break; 	
//////////////////////////////////////////////////////////////////
		case LCD_DATA_STEP:
      lcd_data(0);
      if ((rc=get_key())<=0) return;
      switch(rc) {
	      case KEY_ENTER : 
		      switch(lcd_data_idx%LCD_DATA_PAGE) {
			      case 0 : lcd_data_di_idx=SS[lcd_ss_lno].DIS;
				             lcd_data_di(1); break;
            case 1 : lcd_data_do_idx=SS[lcd_ss_lno].DOS;
				             lcd_data_do(1); break;
				    case 2 : lcd_data_ai_idx=SS[lcd_ss_lno].AIS;
				             lcd_data_ai(1); break;
			    }
			  break;
        case KEY_ESC   : lcd_mmenu(1); break;
        case KEY_RIGHT : lcd_data_idx--; lcd_data(1); break;
        case KEY_DOWN  : lcd_data_idx++; lcd_data(1); break;
      }
    break; 				

//////////////////////////
		case LCD_DATA_DI_STEP:
      lcd_data_di(0);
		  if ((rc=get_key())<=0) return;
        switch(rc) {
			    case KEY_ENTER : lcd_data_di(1); break;
          case KEY_ESC   : lcd_data(1); break;
          case KEY_RIGHT : lcd_data_di_idx-=8; lcd_data_di(1); break;
          case KEY_DOWN  : lcd_data_di_idx+=8; lcd_data_di(1); break;
        }
    break; 	
//////////////////////////
		case LCD_DATA_DO_STEP:
      lcd_data_do(0);
      if ((rc=get_key())<=0) return;
      switch(rc) {
	      case KEY_ENTER : lcd_data_do(1); break;
        case KEY_ESC   : lcd_data(1); break;
        case KEY_RIGHT : lcd_data_do_idx-=8; lcd_data_do(1); break;
        case KEY_DOWN  : lcd_data_do_idx+=8; lcd_data_do(1); break;
      }
    break; 				
//////////////////////////
		case LCD_DATA_AI_STEP:
		  lcd_data_ai(0);
		  if ((rc=get_key())<=0) return;
      switch(rc) {
			  case KEY_ENTER : lcd_data_ai(1); break;
        case KEY_ESC   : lcd_data(1); break;
        case KEY_RIGHT : lcd_data_ai_idx--; lcd_data_ai(1); break;
        case KEY_DOWN  : lcd_data_ai_idx++; lcd_data_ai(1); break;
      }
    break; 		
////////////////////////////////////////////////////////////////
		case LCD_MRS_STEP:
		  lcd_mrs(0);
		  if ((rc=get_key())<=0) return;
      switch(rc) {
  	    case KEY_ENTER : lcd_mrs(1); break;
        case KEY_ESC   : lcd_mmenu(1); break;
        case KEY_RIGHT : lcd_mrs_idx--; lcd_mrs(1); break;
        case KEY_DOWN  : lcd_mrs_idx++; lcd_mrs(1); break;
      }
    break; 				

/////////////////////////////////////////////////////////////////////					
		case LCD_CFG_STEP:
     lcd_cfg(0);
     if ((rc=get_key())<=0) return;
     switch(rc) {
       case KEY_ENTER : lcd_cfg_set(); break;
       case KEY_ESC   : lcd_mmenu(1); break;
       case KEY_RIGHT : lcd_cfg_idx--; lcd_cfg(1); break;
       case KEY_DOWN  : lcd_cfg_idx++; lcd_cfg(1); break;
     }
    break; 	

    case LCD_CFG_SET_STEP:
      switch(rc) {
			  case KEY_ENTER : 
				  LCD_msg[0][X_NUM]=0; LCD_msg[1][X_NUM]=0;
					switch(lcd_cfg_idx) {
						case 0 :
						  sprintf(tlcdmsg,"%s.%s",&LCD_msg[0][8],&LCD_msg[1][0]);
							ret=edit_str_dec(tlcdmsg,ep);
							if (ret==4) {
							  if ((ep[0]>255)||(ep[1]>255)||(ep[2]>255)||(ep[3]>255)) break;
								N_MyIP[0][0] = ep[0]+(ep[1] << 8);             
                N_MyIP[0][1] = ep[2]+(ep[3] << 8);
				        I2C_WriteBytes(I2C_MyIP,(U8 *)N_MyIP[0],4); 
								I2C_ReadBytes(I2C_MyIP,(U8 *)N_MyIP[0],4); 
							}	   
						break;
						
            case 1 :
						  sprintf(tlcdmsg,"%s.%s",&LCD_msg[0][8],&LCD_msg[1][0]);
							ret=edit_str_dec(tlcdmsg,ep);
							if (ret==4) {
							if ((ep[0]>255)||(ep[1]>255)||(ep[2]>255)||(ep[3]>255)) break;;  								   
								N_SubnetMask[0][0] = ep[0]+(ep[1] << 8);             
                N_SubnetMask[0][1] = ep[2]+(ep[3] << 8);  
				        I2C_WriteBytes(I2C_SubnetMask,(U8 *)N_SubnetMask[0],4); 
								I2C_ReadBytes(I2C_SubnetMask,(U8 *)N_SubnetMask[0],4); 
				      }							   
						break;	
							   							   
					  case 2 :
						  sprintf(tlcdmsg,"%s.%s",&LCD_msg[0][8],&LCD_msg[1][0]);
							ret=edit_str_dec(tlcdmsg,ep);
							if (ret==4) {
							  if ((ep[0]>255)||(ep[1]>255)||(ep[2]>255)||(ep[3]>255)) break;
								N_GatewayIP[0][0] = ep[0]+(ep[1] << 8);             
                N_GatewayIP[0][1] = ep[2]+(ep[3] << 8); 
				        I2C_WriteBytes(I2C_GatewayIP,(U8 *)N_GatewayIP[0],4); 
								I2C_ReadBytes(I2C_GatewayIP,(U8 *)N_GatewayIP[0],4); 
						  }
						break;						   
                          
						case 3 :
						  sprintf(tlcdmsg,"%s.%s",&LCD_msg[0][8],&LCD_msg[1][0]);
							ret=edit_str_dec(tlcdmsg,ep);
							if (ret==4) {
							  if ((ep[0]>255)||(ep[1]>255)||(ep[2]>255)||(ep[3]>255)) break;
								N_MyIP[1][0] = ep[0]+(ep[1] << 8);             
                N_MyIP[1][1] = ep[2]+(ep[3] << 8);
				        I2C_WriteBytes(I2C_MyIP2,(U8 *)N_MyIP[1],4); 
								I2C_ReadBytes(I2C_MyIP2,(U8 *)N_MyIP[1],4); 
						  }	   
						break;
						
						case 4 :
						  sprintf(tlcdmsg,"%s.%s",&LCD_msg[0][8],&LCD_msg[1][0]);
							ret=edit_str_dec(tlcdmsg,ep);
							if (ret==4) {
							  if ((ep[0]>255)||(ep[1]>255)||(ep[2]>255)||(ep[3]>255)) break;;  								   
								  N_SubnetMask[1][0] = ep[0]+(ep[1] << 8);             
                  N_SubnetMask[1][1] = ep[2]+(ep[3] << 8);  
				          I2C_WriteBytes(I2C_SubnetMask2,(U8 *)N_SubnetMask[1],4); 
								  I2C_ReadBytes(I2C_SubnetMask2,(U8 *)N_SubnetMask[1],4); 
				      }							   
						break;		
            						   
					  case 5 :
						  sprintf(tlcdmsg,"%s.%s",&LCD_msg[0][8],&LCD_msg[1][0]);
							ret=edit_str_dec(tlcdmsg,ep);
							if (ret==4) {
							  if ((ep[0]>255)||(ep[1]>255)||(ep[2]>255)||(ep[3]>255)) break;
								N_GatewayIP[1][0] = ep[0]+(ep[1] << 8);             
                N_GatewayIP[1][1] = ep[2]+(ep[3] << 8); 
				        I2C_WriteBytes(I2C_GatewayIP2,(U8 *)N_GatewayIP[1],4); 
								I2C_ReadBytes(I2C_GatewayIP2,(U8 *)N_GatewayIP[1],4); 
						  }
						break;
            								   
            case 6 :
						case 7 :
						  lno=lcd_cfg_idx-6;
						  if (LCD_msg[0][11]=='1') MRS[lno].protocol=PROT_TCP;
							else MRS[lno].protocol=PROT_UDP;
		          I2C_WriteBytes(I2C_MRS_PROT+lno*2,(U8 *)&MRS[lno].protocol,2);  
		          I2C_ReadBytes(I2C_MRS_PROT+lno*2,(U8 *)&MRS[lno].protocol,2); 
							   
							MRS[lno].portno=atoi(&LCD_msg[1][7]);
							I2C_WriteBytes(I2C_MRS_PORT+lno*2,(U8 *)&MRS[lno].portno,2); 
							I2C_ReadBytes(I2C_MRS_PORT+lno*2,(U8 *)&MRS[lno].portno,2); 
					  break;

            case 8 :							
							if (LCD_msg[1][8]=='Y') {
							LCD_str(2,1,"Executing !\r");  
							eboot_flag();
					  }
						break;
						   
						case 9 :							
							if (LCD_msg[1][8]=='Y') {
				        LCD_str(2,1,"Executing !\r");  
				        eboot_file(RUN_FILE,FLASH_RUN_ADDR,I2C_FILE_DCRC,I2C_FILE_DSIZE,0);
							}
						break;
						
				   case 10:							
					   if (LCD_msg[1][8]=='Y') {
					     LCD_str(2,1,"Executing !\r");  
					     eboot_file(PLC_FILE,FLASH_PLC_ADDR,I2C_PLC_DCRC,I2C_PLC_DSIZE,0);
					   }
					 break;
						   
				   case 11:							
					   if (LCD_msg[1][8]=='Y') {
					     LCD_str(2,1,"Executing !\r");  
					     eboot_hex(HEX_FILE);
					   }
					 break;
#ifdef SDNP3							   
           case 12 : 
					   plgn[0]->LADDR=atoi(&LCD_msg[1][5]); 
				     I2C_WriteBytes(I2C_D3_LADDR,(U8 *)&plgn[0]->LADDR,2);
						 I2C_ReadBytes(I2C_D3_LADDR,(U8 *)&plgn[0]->LADDR,2);
				   break;
				   
			     case 13 : 
				    plgn[0]->RADDR=atoi(&LCD_msg[1][5]);
			      I2C_WriteBytes(I2C_D3_RADDR,(U8 *)&plgn[0]->RADDR,2);
						I2C_ReadBytes(I2C_D3_RADDR,(U8 *)&plgn[0]->RADDR,2);
					 break;
					
           case 14:
					 case 15:
			     lno=lcd_cfg_idx-14;
			     if (LCD_msg[0][11]=='1') prsn[lno]->ptcpudp=1;  else prsn[lno]->ptcpudp=0;
						 I2C_WriteBytes(I2C_D3_PROT+lno*2,(U8 *)&prsn[lno]->ptcpudp,2);
					   I2C_ReadBytes(I2C_D3_PROT+lno*2,(U8 *)&prsn[lno]->ptcpudp,2);
					 
						 prsn[lno]->portno=atoi(&LCD_msg[1][7]);							   
						 I2C_WriteBytes(I2C_D3_PORT+lno*2,(U8 *)&prsn[lno]->portno,2);
					   I2C_ReadBytes(I2C_D3_PORT+lno*2,(U8 *)&prsn[lno]->portno,2);		
					 break;								 
#endif								 
					}
				break;			   					   					      

        case KEY_ESC   : 
				default : break;
      }
			lcd_cfg(1);
    break; 				
			
///////////////////////////////////////////////////////////////////
    case LCD_PWR_SET_STEP:
      switch(rc) {
			  case KEY_ENTER : 
				  LCD_msg[0][X_NUM]=0; LCD_msg[1][X_NUM]=0; 
					ep[0]=atoi(&LCD_msg[0][9]);
					if (ep[0]==815) {
					  pwr_set=1;
            lcd_step=pwr_run_step; return;
					}
        break;			
		  }
			lcd_mmenu(1); 
		break;
////////////////////////////////////////////////////////////////					
		
		default :
		  lcd_step=0;
		  lcd_edit=0;  
		break;			
  } 

}

void init_lcd(void)
{  	
    LCD_comm(0x30);
    LCD_comm(0x30);
    LCD_comm(0x30);
    LCD_comm(0x38);
    LCD_comm(0x80);
    LCD_comm(0x01);
    LCD_comm(0x06);
    LCD_comm(0x0C);
 	
	LCD_LIGHT(0);
#ifdef LCD_RDY_CHK_PRO    	
    LcdLightOffSec=300-10;	lcd_rdy=1; 
#else	
	lcd_rdy=1; 
#endif	
}


#endif

